package com.example.basic.googleAPI;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;

public class GsonUtil {

    /*
    {
    "client": {
      "clientId":      "yourcompanyname",
      "clientVersion": "1.5.2"
    },
    "threatInfo": {
      "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
      "platformTypes":    ["WINDOWS"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [
        {"url": "http://www.urltocheck1.org/"},
        {"url": "http://www.urltocheck2.org/"},
        {"url": "http://www.urltocheck3.com/"}
      ]
    }
  }
     */
    public static String getRequestApiBodyJsonStr(String targetUrl) {
        ThreatEntry[] threatEntries = new ThreatEntry[1];
        threatEntries[0] = new ThreatEntry(targetUrl);
        return getRequestApiBodyJsonStr(threatEntries);
    }

    public static String getRequestApiBodyJsonStr(ArrayList<String> targetUrl) {
        ThreatEntry[] threatEntries = new ThreatEntry[targetUrl.size()];
        for (int i = 0; i < targetUrl.size(); i++) {
            threatEntries[i] = new ThreatEntry(targetUrl.get(i));
        }
        return getRequestApiBodyJsonStr(threatEntries);
    }

    private static String getRequestApiBodyJsonStr(ThreatEntry[] threatEntries) {
        ApiRequestBody apiRequestBody = new ApiRequestBody();
        apiRequestBody.client.put("clientId","cqttech");
        apiRequestBody.client.put("clientVersion","1.0.13.1");

        apiRequestBody.threatInfo.put("threatTypes",new String[]{"MALWARE", "SOCIAL_ENGINEERING"});
        apiRequestBody.threatInfo.put("platformTypes",new String[]{"ALL_PLATFORMS"});
        apiRequestBody.threatInfo.put("threatEntryTypes",new String[]{"URL"});

        apiRequestBody.threatInfo.put("threatEntries",threatEntries);

        Gson gson = new Gson();
        String json = gson.toJson(apiRequestBody);
//        System.out.println("请求体：\n" + json + "\n ------------- end -------------");
        return json;
    }

    public static void main(String[] args) {
        MatchesResult matchesResult = new MatchesResult();
        MatchesResult.ThreatEntry apiRequestBody = new MatchesResult.ThreatEntry("http://tanahsucikn.com/");
        matchesResult.setMatches(new MatchesResult.Result[]{
                new MatchesResult.Result("SOCIAL_ENGINEERING","ALL_PLATFORMS","300s","URL",apiRequestBody)});
        Gson gson = new Gson();
        String s = gson.toJson(matchesResult);
        System.out.println(s);
    }


    static class ApiRequestBody {
        public HashMap<String,String> client = new HashMap<>();
        public HashMap<String,Object> threatInfo = new HashMap<>();
    }

    static class ThreatEntry {
        public String url;

        public ThreatEntry() {
        }

        public ThreatEntry(String url) {
            this.url = url;
        }
    }
}
